﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    public CharacterBase player;
    public float damage = 1.0f;
    public float speed = 1.0f;
    public float duration = 5.0f;

    private void Start()
    {
        player = GameObject.Find("Base").GetComponent<CharacterBase>();
    }
    public void Update()
    {
        Lauch();
    }

    private void OnEnable()
    {
        GetComponent<BoxCollider2D>().enabled = true;
        if (duration > 0)
        {
            StartCoroutine(DestroyThis());
        }
    }

    private void OnDisable()
    {
        GetComponent<BoxCollider2D>().enabled = false;
    }

    public void Lauch()
    {
        this.transform.Translate(new Vector2(speed * Time.deltaTime, 0), Space.Self);
    }

    public void Damage(Collision2D _collision, float _damage)
    {
        /*
        if (_collision.gameObject.GetComponent<MonsterBase>())
        {
            _collision.gameObject.GetComponent<MonsterBase>().Hurt(10);
        }*/
    }

    public IEnumerator DestroyThis()
    {
        yield return new WaitForSeconds(duration);
        ObjectPool.Instance.InPool(gameObject);
    }//子弹销毁

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(collision.gameObject.tag == "Monster")
        {
            Damage(collision,damage);
            player.vampireCount++;
            ObjectPool.Instance.InPool(gameObject);
        }
        if(collision.gameObject.tag == "Floor")
        {
            ObjectPool.Instance.InPool(gameObject);
        }
    }
}
